package com.hcy.utils;

import com.hcy.entity.Baidu;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: LiJun
 * @Date: 2025/7/23 15:20
 */
@Slf4j
public class SpiderUtil {
    private static String baiduHotUrl = "https://top.baidu.com/board?tab=realtime";
    public static List<Baidu> spiderBaidu() {
        List<Baidu> baiduList = new ArrayList<>();
        try {
            Document dc = Jsoup.connect(baiduHotUrl).get();
            Integer rank = 0;
            for (Element element : dc.getElementsByClass("category-wrap_iQLoo horizontal_1eKyQ")) {
                String title = element.getElementsByClass("c-single-text-ellipsis").text();
                String picUrl = element.select("img").attr("src");
                String link = element.getElementsByClass("img-wrapper_29V76").select("a").attr("href");
                String hotIndex = element.getElementsByClass("hot-index_1Bl1a").text();
                Baidu baidu = new Baidu();
                baidu.setTitle(title);
                baidu.setPicUrl(picUrl);
                baidu.setLink(link);
                baidu.setRank(rank.toString());
                baidu.setHotIndex(hotIndex);
                rank++;
                baiduList.add(baidu);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return baiduList;
    }
}
